<?php
/**
 * 调用方式
 * include("comm.config.php");
 * include(COREROOT."/system/svn/svnsync.php");
 * 
 */
require 'define.php';
ini_set('display_errors', 'on');
//初始化
//if(!isset($_SESSION)){ func_initSession();session_start();}

//检查权限
//checkAuth();

$str = null;
$parent=null;
$path=null;
$version=null;

//加载变量
if ( !empty ( $_POST )) {
	$str = mysvn($_POST["path"]);
	$parent=$_POST["parent"];
	$path=$_POST["path"];
	$version=$_POST["version"];
	if(empty($str)){
		$str="SVN 同步失败！";
	}
}

//加载路径信息
$list = Files::fileList( BASE_DIR );
$target = array();
$target[]=".";

foreach($list as $value){
	if( is_dir( BASE_DIR.$value ) && is_dir( BASE_DIR.$value."/.svn" )  ){
		$target[] = $value;
	}
}

//SVN同步操作
function mysvn($path) {
	//$config=conf("svn");
	if(substr($path,0,1)=='/'){
		$path=substr($path,1);
	}
	$v=intval($_POST["version"]);
	$revision = empty( $v )?"":"/rev:".$_POST["version"];
	$filename = ($_POST['parent']=='.')?BASE_DIR.$path : BASE_DIR.$_POST['parent']."/".$path;
	//$output = shell_exec("svn update  $filename $revision --username=mejinke --password=tianjinke");
	echo $filename;exit;
	$ouput = exec('TortoiseProc.exe /command:update /path:"'.$filename.'" '.$revision.' /closeonend:1');
	//$output .= shell_exec("sudo chown -R {$config['user']}:{$config['group']} {$filename}");

	if(!empty($output)){
		//svnlog($output);
	}
	return $output;
}

//SVN日志操作
function svnlog($output){
	$folder = BASE_DIR . "/logs/svn/".date("y-m-d",time())."/";
	if( !file_exists( $folder) ){
		Files::mkdirs( $folder);
	}
	file_put_contents( $folder. $_SESSION["UserID"] . "_" .date("H-i-s",time()) .".log", $output);
}

//权限认证
function checkAuth($level=1,$ids=array(0,1)){

	$html="<h1>Access Denied!</h1>";

	//是否登录
	if(empty($_SESSION ["UserID"])){
		echo $html;exit;
	}
	if($level==1){
		return true;
	}

	//是否为管理员
	if( $_SESSION["UserType"]!=1 ){
		echo $html;exit;
	}
	if($level==2){
		return true;
	}

	if( !in_array($_SESSION ["UserID"], $ids)){
		echo $html;exit;
	}


	return true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SVN 同步设置</title>
	<style>
		#post{
			float:left;
			width:auto;
			margin: 0px auto 0px auto; 
			padding: 0px;			
		}
		#content{
			margin: 0px auto 0px auto; 
			padding: 0px 20px 0px 20px;
		}
		hr{
			width:99%;
		}
		strong{
			color:red;
		}
	</style>	
	<script type=text/javascript>
	<!--
	function checkfrom(){
		if(document.getElementById('parent').value==""){
			window.alert("请选择要同步的父目录");
			return false;
		}else if(isNaN(document.getElementById("version").value)){
			window.alert('版本信息必须是数字！');
            document.getElementById("version").select();
            document.getElementById("version").focus();
            return false;
		}else if(document.getElementById('path').value==""||document.getElementById('path').value=="/"){
			if(checkclick("确认同步整个 ‘"+getParentPath(document.getElementById('parent').value)+"’ 目录么？")){
				return true;
			}else{
				document.getElementById('parent').focus;
				return false;
			}
		}else{
			return true;
		}	
	}
	function getParentPath(pathinfo){
		if(pathinfo=='.'){
			return "站点根目录";
		}
		return pathinfo;
	}
	function checkclick(msg){
	    if (confirm(msg)) {
	        return true;
	    }
	    else {
	        return false;
	    }
	}
	-->
	</script>
	<?php if(!empty($eefocus)){ ?>
	<style>
		#box{
			width:98%;
			height:24px;		
			padding:5px;
			margin:0px auto 6px auto;			
			line-height:24px;
			clear:both;
		}
		input,select,span,div{
			font-size:12px;
		}
	</style>
	<link href="/css/admin/basic.css" rel="stylesheet" type="text/css"/>
    <link href="/css/admin/<?php echo USERFACE;?>.css" rel="stylesheet" type="text/css"/>
	<?php }else{?>
	<style>
		#box{
			background-color:#E5E5E5;
			width:98%;
			height:30px;		
			padding:5px;
			margin:0px auto 6px auto;			
			line-height:30px;
			clear:both;
		}
		input,select,span,div{
			font-size:16px;
		}
	</style>
	<?php } ?>
</head>
<body>
	<?php if(!empty($eefocus)){ ?><div id="top">
	  <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0" class="dhbg">
	    <tr>
	      <td align="left">&nbsp;&nbsp;<span class="whitelink">版本同步</span></td>
	      <td align="right" class="whitestyle">&nbsp;&nbsp;</td>
	    </tr>
	  </table>
	  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="bline">
	    <tr>
	      <td><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
	          <tr>
	            <td height="24">您的位置：>> 版本同步 >> 服务器 <?php echo $_SERVER["SERVER_NAME"]; ?></td>
	            <td align="right"><a href="<?php $returnstr=empty($_SERVER["HTTP_REFERER"])?"":$_SERVER["HTTP_REFERER"];echo $returnstr;?>" class='tishilink'>返回</a></td>
	          </tr>
	        </table></td>
	    </tr>
	  </table>
	  <table width="100%" height="6"  border="0" align="center" cellpadding="0" cellspacing="0">
	    <tr>
	      <td></td>
	    </tr>
	  </table>
	</div>
	<?php } ?>
	<div id='box'>
		<span style="float:left;"><strong><?php echo $_SERVER["SERVER_NAME"] ?></strong> SVN 同步设置:</span>	
		<form name="svn" id="post" method="post" action="./<?php echo basename($_SERVER['SCRIPT_NAME'])?>" onsubmit="return checkfrom();">			
			<select name="parent" id="parent">
				<option value="">请选择目录</option>
				<?php foreach ($target as $value){ ?>
				<option value="<?php echo $value;?>"
				<?php if($value==$parent) {echo "selected";}?>><?php echo $value;?></option>
				<?php }?>
			</select> 			
			<input type="text" name="path" id="path" value="<?php echo $path;?>" size="35" title="要更新的目标文件"/> 
			<input type="text" name="version" id="version" value="<?php echo $version;?>" size="5" title="目标版本，留空为最新版本"/>
			<input type="submit" name="submit" value="提交" />		
		</form>
	</div>
	<hr style="clear:both;">
	<div id="content">
	<?php
		if(!empty( $str )){
			echo "<pre>";
			echo $str;
			echo "</pre>";
		}
	?>
	</div>	
	<br>
	<br>
</body>
</html>
